#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "CampaignEditForm.h"
//#include "CharacterEditForm.h"
#include "ClassEditForm.h"

#include <CampaignItem.h>
#include <CharacterItem.h>
#include <FileHandler.h>
#include <XmlSerializer.h>



#include <QFileDialog>
#include <QMessageBox>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    installEventFilter(this);
    FileHandler::installSerializer(new XmlSerializer(this));

    ui->setupUi(this);
    ui->menuCampaign->setEnabled(false);
    ui->menuCharacter->setEnabled(false);
}



MainWindow::~MainWindow()
{
    delete ui;
}



bool MainWindow::eventFilter(QObject *o, QEvent *e)
{
    if (o != this) return false;
    if (e->type() != QEvent::Close) return false;

    on_actionClose_All_triggered();

    return false;
}


void MainWindow::onUpdateTabs()
{
    for (int index = 0; index < ui->tabWidget->count(); index++)
    {
        AbstractEditFormActions* form = dynamic_cast<AbstractEditFormActions*>(ui->tabWidget->widget(index));
        if (!form) continue;

        ui->tabWidget->setTabText(index, form->root()->name());
    }
}



void MainWindow::on_actionOpen_triggered()
{
    QString file_path = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath(), tr("Xml File (*.xml)"));
    if (file_path.isEmpty() || !QFile::exists(file_path)) return;

    ItemRoot* root = FileHandler::open(file_path);

    if (dynamic_cast<CampaignItem*>(root))
    {
        CampaignEditForm* form = new CampaignEditForm(root, this);
        ui->tabWidget->addTab(form, root->name());
        ui->tabWidget->setCurrentWidget(form);
        connect(form->root(), SIGNAL(sigDirty()), this, SLOT(onUpdateTabs()));
        return;
    }

    if (dynamic_cast<CharacterItem*>(root))
    {
        //CharacterEditForm* form = new CharacterEditForm(this);

        ClassEditForm* form = new ClassEditForm(root, this);
        ui->tabWidget->addTab(form, root->name());
        ui->tabWidget->setCurrentWidget(form);
        connect(form->root(), SIGNAL(sigDirty()), this, SLOT(onUpdateTabs()));
        return;
    }

    QMessageBox::information(this, tr("Unknown Xml Content!"), tr("File has unknown file content!"));
}



void MainWindow::on_actionNew_Character_Class_triggered()
{
    ClassEditForm* form = new ClassEditForm(new CharacterItem(tr("New Character Class")), this);
    ui->tabWidget->addTab(form, form->root()->name());

    connect(form->root(), SIGNAL(sigDirty()), this, SLOT(onUpdateTabs()));
}



void MainWindow::on_actionNew_Campaign_Setting_triggered()
{
    CampaignEditForm* form = new CampaignEditForm(new CampaignItem(tr("New Campaign Setting")), this);
    ui->tabWidget->addTab(form, form->root()->name());

    connect(form->root(), SIGNAL(sigDirty()), this, SLOT(onUpdateTabs()));
}



void MainWindow::on_actionSave_triggered()
{
    AbstractEditFormActions* form = dynamic_cast<AbstractEditFormActions*>(ui->tabWidget->currentWidget());
    if (!form) return;

    form->save();
}



void MainWindow::on_actionSave_As_triggered()
{
    AbstractEditFormActions* form = dynamic_cast<AbstractEditFormActions*>(ui->tabWidget->currentWidget());
    if (!form) return;

    form->saveAs();
}



void MainWindow::on_actionSave_All_triggered()
{
    for (int index = 0; index < ui->tabWidget->count(); index++)
    {
        AbstractEditFormActions* form = dynamic_cast<AbstractEditFormActions*>(ui->tabWidget->widget(index));
        if (!form) continue;

        form->save();
    }
}



void MainWindow::on_actionClose_triggered()
{
    on_tabWidget_tabCloseRequested(ui->tabWidget->currentIndex());
}



void MainWindow::on_actionClose_All_triggered()
{
    int count = ui->tabWidget->count();
    while (count)
    {
        on_tabWidget_tabCloseRequested(0);

        if (count == ui->tabWidget->count()) return;
        count = ui->tabWidget->count();
    }
}


void MainWindow::on_actionQuit_triggered()
{
    close();
}


void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    QWidget* widget = ui->tabWidget->widget(index);
    AbstractEditFormActions* form = dynamic_cast<AbstractEditFormActions*>(ui->tabWidget->widget(index));
    if (!form)
    {
        ui->tabWidget->removeTab(index);
        return;
    }


    if (!form->changed())
    {
        ui->tabWidget->removeTab(index);
        widget->close();
        return;
    }


    switch (QMessageBox::question(this, tr("Save Changes?"), tr("File has been modified.\nDo you want to save file?"), QMessageBox::Yes | QMessageBox::No ))
    {
    case QMessageBox::Yes: form->save();
    }

    ui->tabWidget->removeTab(index);
    widget->close();
}





void MainWindow::on_tabWidget_currentChanged(int index)
{
    ui->menuCampaign->clear();
    ui->menuCharacter->clear();

    ui->menuCampaign->setEnabled(false);
    ui->menuCharacter->setEnabled(false);


    QWidget* current = ui->tabWidget->widget(index);
    if (dynamic_cast<CampaignEditForm*>(current))
    {
        ui->menuCampaign->addActions(current->actions());
        ui->menuCampaign->setEnabled(true);
        return;
    }

    if (dynamic_cast<ClassEditForm*>(current))
    {
        ui->menuCharacter->addActions(current->actions());
        ui->menuCharacter->setEnabled(true);
        return;
    }
}
